package edu.gmu.foxhound;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	SharedPreferences settings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		settings = this.getSharedPreferences(this.getString(R.string.settings), Context.MODE_PRIVATE);
		//initial settings setup
		
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(this.getString(R.string.settings_toast_mode_silent), Boolean.parseBoolean(this.getString(R.string.settings_toast_mode_silent_val)));
		editor.putLong(this.getString(R.string.settings_alarm_interval), Long.parseLong(this.getString(R.string.settings_alarm_interval_val)));
		editor.commit();
		settings = this.getSharedPreferences(this.getString(R.string.settings), Context.MODE_PRIVATE);

	}

	public void setAlarm_onClick (View view){
		//sets up the alarm

		
		System.out.println("Creating alarm in main");
		long time = settings.getLong(this.getString(R.string.settings_alarm_interval), 
				Long.parseLong(this.getString(R.string.settings_alarm_interval_val)));
		System.out.println("time: "+time);
		Util.CreateAlarm(
				this, 
				time
				);
				
		
		/*
		System.out.println("Finding trigger time");
		
		long triggerTime = settings.getLong(this.getString(R.string.settings_alarm_interval), 
				Long.parseLong(this.getString(R.string.settings_alarm_interval_val)));
		System.out.println("time: "+triggerTime);
		
		System.out.println("About to create alarm intent");
		
		//create Intent and PendingIntent to send to alarm
    	Intent intent = new Intent(this.getString(R.string.intent_action));
    	PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    	
    	System.out.println("getting alarm service");
    	
    	//get alarm service and create alarm
    	AlarmManager alrmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    	alrmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + triggerTime, pendingIntent);
    	
    	System.out.println("Alarm created");
			*/	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public boolean dispatchKeyEvent(KeyEvent event){
		int keyCode = event.getKeyCode();
		System.out.println("KeyCode: "+event);
		//System.out.println("KeyEvent: "+KeyEvent.KEYCODE_VOLUME_UP);
		//System.out.println("KeyEvent: "+KeyEvent.KEYCODE_VOLUME_DOWN);
		//System.out.println("KeyEvent: "+KeyEvent.KEYCODE_POWER);
		
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			
			/*
			System.out.println("VOL_UP");
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(this.getString(R.string.settings_toast_mode_silent), false);
			editor.commit();
			*/
			
			this.startActivity(new Intent().setClass(this, SettingsActivity.class));
			return true;
		}
		/*
		else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			System.out.println("VOL_DOWN");
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(this.getString(R.string.settings_toast_mode_silent), true);
			editor.commit();
			return true;
		}
		else if(keyCode == KeyEvent.KEYCODE_POWER){
			System.out.println("POWER");
			//if the alarm interval is not set
			if (settings.getLong(this.getString(R.string.settings_alarm_interval), 0L) == 0L){
				
				//set alarm interval
				SharedPreferences.Editor editor = settings.edit();
				editor.putLong(this.getString(R.string.settings_alarm_interval), Long.parseLong(this.getString(R.string.settings_alarm_interval_val)));
				editor.commit();
				
				//create alarm
				Util.CreateAlarm(
						this, 
						settings.getLong(this.getString(R.string.settings_alarm_interval), 
								Long.parseLong(this.getString(R.string.settings_alarm_interval_val)))
						);
			}
			else{
				
				//disable alarm 
				SharedPreferences.Editor editor = settings.edit();
				editor.putLong(this.getString(R.string.settings_alarm_interval), 0L);
				editor.commit();
			}
		
			
			return true;
		}
		 */
		//catches other events
		return super.dispatchKeyEvent(event); 
		
	}

}
